// Callback method that reports back to the test case.
var respondWithId = function(responseId, arg) {
  gadgets.rpc.call(null, 'jsauto-appbridge-response', undefined,
      responseId, arg);
};

var sendErrorWithId = function(responseId, arg) {
  gadgets.rpc.call(null, 'jsauto-appbridge-error', undefined,
      responseId, arg);
};

var sendAck = function() {
  respondWithId('ack', null);
};

gadgets.rpc.register('jsauto-appbridge-request',
    function(requestId, code) {
      var respond = function(arg) {
        respondWithId(requestId, arg);
      };
      try {
        try {
          eval(code);
        } finally {
          if (this['callback']) {
            this['callback']();
          }
        }
      } catch (ex) {
        sendErrorWithId(requestId, ex.toString());
      }
    });
